package com.xixi.easy.rpc.server;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;

/**
 * @author: zxh
 * @date: 2024/8/20 23:08
 * @description: 基于Vert.x实现的web服务器
 */
public class VertxHttpServer implements CustomHttpServer {

	@Override
	public void doStart(int port) {
		// 创建Vert.x实例
		Vertx vertx = Vertx.vertx();

		// 创建Http服务器
		HttpServer server = vertx.createHttpServer();

		// 监听端口并处理请求
		server.requestHandler(new CustomHttpServerHandler());
		/*server.requestHandler(request -> {
			// 处理HTTP请求
			System.out.println("Received request:" + request.method() + " " + request.uri());

			// 发送HTTP响应
			request.response()
					.putHeader("content-type", "text/plain")
					.end("Hello from Vert.x Http Server!");
		});*/

		// 启动HTTP服务器并监听指定端口
		server.listen(port, result -> {
			// 启动服务成功与失败的处理
			if (result.succeeded()) {
				System.out.println("Server is now listening on port " + port);
			} else {
				System.out.println("Failed to start server: " + result.cause());
			}
		});
	}
}
